<?php

namespace app\api\controller;

use app\admin\controller\Goods;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function sign($data){
      $key=config("site.key");
      foreach ($data as $key => $value) {
        if (empty($value)) {
          unset($data[$key]);
        }
      }
      ksort($data);

      $md5str = "";
      foreach ($data as $key => $val) {
        $md5str = $md5str . $key . "=" . $val . "&";
      }
      $sign = strtoupper(md5($md5str . "key=" . $key));
      return $sign;
    }
    /**
     * 首页
     *
     */
    public function index()
    {

      $goods_model=new \app\admin\model\Goods();
      $page=$this->request->post("page",1);
      $listrow=$this->request->post("pageSize",50);
      $keywords=$this->request->post("keywords",'');
      $timestamp=$this->request->post("timestamp",'');
      $sign = $this->request->post('sign',"");

      if(!$sign){
        $this->error("签名参数不能为空");
      }
      $mysign=$this->sign(["page"=>$page,"pageSize"=>$listrow,"keywords"=>$keywords,"timestamp"=>$timestamp]);

      if($mysign!=$sign){
        $this->error("签名不正确");
      }
      $where['id']=['gt',0];
      if($keywords){
        $where['keywords']=['like', '%' . $keywords . '%'];
      }
      $list=$goods_model
        ->field("id,keywords,good_title,thumb,collect_time")
        ->where($where)
        ->order("id desc" )
        ->paginate($listrow,true,["page"=>$page]);
      if($list){
        $this->success('请求成功',$list);
      }else{
        $this->success('请求失败');
      }

    }


    public function detail()
    {
      $id=$this->request->post("id");

      if(!$id){
        $this->error("请求失败，请求商品编号不能为空");
      }
      $timestamp=$this->request->post("timestamp",'');
      $sign = $this->request->post('sign',"");
      if(!$sign){
        $this->error("签名参数不能为空");
      }
      $mysign=$this->sign(["id"=>$id,"timestamp"=>$timestamp]);
      if($mysign!=$sign){
        $this->error("签名不正确");
      }
      $goods_model=new \app\admin\model\Goods();
      $goods_info=$goods_model->find($id);
      if($goods_info){
        $this->success('请求成功',$goods_info);
      }else{
        $this->error('请求失败，未找到相关商品数据');
      }

    }
}
